//开机所有段码显示闪三次
//RTD_Settings 开机后一直闪耀, 按下旋钮按键后, 显示实际温度并开始温控
//按下旋钮按键后, 显示所有设定值, 从RTD_Settings开始闪耀,没任何操作,闪5次后隐藏

//闪耀,Settings更改后延迟1s闪耀
//WIFI未连接闪耀显示


// #include <stdint.h>
#include "UI.h"
#include "../MCU_Mold/MCU_Time.h"
#include "../MCU_Mold/MCU_AbstractTube.h"
#include "HT162X_Parts.h"
#include "HT162X_Parts_Num.h"
#include "HT162X_Pin.h"

UI_System_S UI_System;
#define Ver_ShowTime 100
uint16_t Ver_ShowCount = Ver_ShowTime;
uint16_t UI_Refresh_Count = 0; //Tick 10ms
extern uint16_t Power_State;
extern bool F_C_flag;
//10ms
void UI_Clock(void)
{
    static uint8_t x = 0;
    static uint8_t x2 = 0;
    static uint8_t x3 = 0;
    static uint8_t x4 = 0;

    x4++;
    if(x4 > 40)
    { //400ms
        x4 = 0;
        if(UI_System.Shine_WIFI == Shine_ON) UI_System.Shine_WIFI = Shine_OFF;
        else UI_System.Shine_WIFI = Shine_ON;
    }

    if(UI_System.State_Operation == UI_None)
    {
        if(x > 0) x = 0;
        x3++;
        if(x3 >= 50)
        { //500ms
            x3 = 0;
            if(UI_System.Shine_Colon == Shine_ON) UI_System.Shine_Colon = Shine_OFF;
            else UI_System.Shine_Colon = Shine_ON;
        }

        x2++;
        if(x2 >= 100)
        { //1s
            x2 = 0;
            if(UI_System.Timer1.Actual > 0)
            {
                UI_System.Timer1.Actual--;
                if(UI_System.Timer1.Actual == 0)
                {
                    UI_System.Alarm_Timer = Alarm_Timer1; //Timer Alarm Enable
                    UI_System.Timer1.Settings = 0;
                    UI_System.Timer1.Alart_State = Alart_Action;
                }
            }
        }
        return;
    }
    else
    {
        if(x2 > 0) x2 = 0;
        if(x3 > 0) x3 = 0;
    }
}

void UI_Init(void)
{
    UI_System.UI_Show = UI_OFF;
    UI_System.State_TempCtrl = UI_OFF;
    UI_System.ICO_WIFI = UI_OFF;
    UI_System.ICO_Bluetooth = UI_ON;
    UI_System.ICO_Auger = UI_OFF;
    UI_System.ICO_Fan = UI_OFF;
    UI_System.ICO_Heating = UI_OFF;
    UI_System.ICO_Error = UI_OFF;
    UI_System.ICO_Cell = UI_OFF;
    UI_System.State_Operation = UI_None;
    UI_System.Grill_Unit = Unit_F;

    Temp_Param.oven = 50;
    Temp_Param.oven_set = 180;
    Temp_Param.probe1 = 0;
    Temp_Param.probe1_set = Alarm_Probe_Min;
    Temp_Param.probe2 = 0;
    Temp_Param.probe2_set = Alarm_Probe_Min;
    UI_System.Timer1.Actual = 0;
    UI_System.Timer1.Settings = 0;

    UI_System.Alarm_Timer = Alarm_None;
    UI_System.Alarm_RTD = Alarm_None;
    UI_System.Alarm_ProbeA = Alarm_None;
    UI_System.Alarm_ProbeB = Alarm_None;

    Lcd_Parts_Init();
	Flash_Read();
}

void UI_unit_show(void)
{
    if(UI_System.Grill_Unit == Unit_F)
    {
        Lcd_F(Char_ON);
        Lcd_C(Char_OFF);
    }
    else if(UI_System.Grill_Unit == Unit_C)
    {
        Lcd_F(Char_OFF);
        Lcd_C(Char_ON);
    }
    else if(UI_System.Grill_Unit == Unit_F_C)
    {
        Lcd_F(Char_ON);
        Lcd_C(Char_ON);
    }
}
float temp_RTD = 0xffffffff;
void UI_Refresh(void)
{
    MCU_Time_T t = {0,0,0};
    uint16_t temp = 0;
    uint16_t temp_FC = 0;
    
    static float temp_Probe1 = 0xffffffff;
    static float temp_Probe2 = 0xffffffff;
    
    if(UI_System.UI_Show == UI_ON)
    {
        KeyLed_ON;
        if(Temp_Param.oven_set != Temp_Shutoff && Power_State == 1)
        {
            Lcd_Backlight_ON;
			FanCtrlStart();
            if(Temp_Param.oven_Err == Pt_Normal)
            {
                Lcd_Probe1_Show(Char_ON);
                Lcd_Probe2_Show(Char_ON);
                Lcd_Actual(Char_ON);
                Lcd_F(Char_ON);
                Lcd_Cook(Char_ON);
                if(UI_System.Timer1.Actual > 0)
                { //Shutdown OFF
                    if(UI_System.Shine_Colon == Shine_ON) Lcd_Timer_Colon(Char_ON);
                    else Lcd_Timer_Colon(Char_OFF);
                }
                else
                {
                    if(temp_RTD == 0xffffffff) temp_RTD = Temp_Param.oven; 
                    if(temp_Probe1 == 0xffffffff) temp_Probe1 = Temp_Param.probe1; 
                    if(temp_Probe2 == 0xffffffff) temp_Probe2 = Temp_Param.probe2; 
                    Lcd_Timer_Colon(Char_OFF);
                    if(UI_Refresh_Count == 0) 
					{
						UI_Refresh_Count = 1;
						temp_RTD = Temp_Param.oven;
						temp_Probe1 = Temp_Param.probe1;
						temp_Probe2 = Temp_Param.probe2;
					}
						
                    if(UI_Refresh_Count >= 100)
                    { //Tick 1s
                        UI_Refresh_Count = 0;
                        
                    }

                    // temp_FC = ConvertC(temp_RTD);
                    Lcd_NumShow(UI_RTD,temp_RTD);
                    // temp_FC = ConvertC(Temp_Param.oven_set);
                    Lcd_NumShow(UI_RTDSettings,Temp_Param.oven_set);
                    // temp_FC = ConvertC(temp_Probe1);
                    Lcd_NumShow(UI_ProbeA,temp_Probe1);
                    // temp_FC = ConvertC(temp_Probe2);
                    Lcd_NumShow(UI_ProbeB,temp_Probe2);
                    UI_unit_show();
                }
                FanCtrlStart();
            }
            else
            { //只显示Oven的Er
                HT162X_ResetRAM();
                Lcd_NumShow(UI_RTD,0);
            }
            HT162X_RAM_Tx();
        }
        else if(Temp_Param.oven_Err == Pt_Normal)
        {	
			
			FanCtrlStop();
			
			Lcd_Timer_Colon(Char_ON);
			Lcd_Number(ExhaustFanClock/600,Timer_seg2);
			Lcd_Number(ExhaustFanClock/60%10,Timer_seg3);
            Lcd_Number(ExhaustFanClock%60/10,Timer_seg4);
            Lcd_Number(ExhaustFanClock%10,Timer_seg5);
			
            Lcd_Number(0,TempAct_seg1);
			Lcd_Number(Tube_Encoder_F,TempAct_seg2);
			Lcd_Number(Tube_Encoder_F,TempAct_seg3);
			HT162X_RAM_Tx();
        }
    }
    else
    {
        KeyLed_OFF;
        temp = 1;
    }
    if(temp == 1)
    {
        Lcd_Backlight_OFF;
        HT162X_ResetRAM();
        FanCtrlStop();
        ADInit();
        
        HT162X_RAM_Tx(); 
    }
}

void UI_Ver()
{
    if(Switch_State == Temp_Shutoff)
    {
        Lcd_Number(6,TempAct_seg1); //代表 马达转速
        Lcd_Number(0,TempAct_seg2); //版本号
        Lcd_Number(2,TempAct_seg3);

        HT162X_RAM_Tx();
    }
}
//Tick 10ms
void UI_Ver_Clock()
{
    if(Switch_Active == 0) return;

    if((Ver_ShowCount > 0) && (Ver_ShowCount < 0xffff))
    {
        Ver_ShowCount--;
        if(Switch_State == Temp_Shutoff)
        {
            if(Ver_ShowCount == 0) Lcd_Backlight_OFF;
            else if(Ver_ShowCount >= (Ver_ShowTime - 5))
            {
                HT162X_ResetRAM();
                Lcd_Backlight_ON;
            }
        }
    }
}

void UI_Main(void)
{
    if(Switch_Active == 0) return;
    
    if(Ver_ShowCount == 0) UI_Refresh();
    else UI_Ver();
}
